<?php
/**
 *
 * User: 梁晓伟  lxw11109@gmail.com
 * Date: 2017-11-05
 * Time: 16:37
 */

namespace App\Http\Controllers\Api\V1\Manager\System;


use App\Http\Controllers\Controller;
use App\Models\Admin;
use App\Models\Brand;
use App\Models\Good;
use App\Models\GoodsOneType;
use App\Models\GoodsTwoType;
use App\Models\HelpCenter;
use App\Models\IntegralGood;
use App\Models\Message;
use App\Models\MessageUser;
use App\Models\Order;
use App\Models\OrderDetail;
use App\Models\Supplier;
use App\Models\UserHome;
use App\Models\UserStatistics;
use App\Models\VipLog;
use DateTime;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class HelpController extends Controller
{

    private $namespace = "App\Http\Controllers\Api\V1\Manager\System\HelpController";

    public function index(Request $request)
    {
        $pageSize = $request->pageSize;
        $sql = HelpCenter::select('t1.id', 't1.name', 't1.createtime', 't1.updatetime', 't2.nickname as admin_name')
            ->from((new HelpCenter())->getTable() . ' as t1')
            ->leftJoin((new Admin())->getTable() . ' as t2', 't1.admin_id', '=', 't2.id');
        $helpList = $sql->orderByDesc('t1.updatetime')
            ->paginate($pageSize)
            ->toArray();
        $data['helpList'] = $helpList['data'];
        foreach ($data['helpList'] as $key => $value) {
            if (isset($value['createtime'])) {
                $data['helpList'][$key]['createtime'] = date("Y-m-d H:i:s", $value['createtime']);
            }
            if (isset($value['updatetime'])) {
                $data['helpList'][$key]['updatetime'] = date("Y-m-d H:i:s", $value['updatetime']);
            }
        }
        $data['totalNum'] = $helpList['total'];
        return $this->apiResponse($data);
    }

    public function show(Request $request, $id)
    {
        $help = HelpCenter::select('t1.id', 't1.name', 't1.content', 't1.createtime', 't1.updatetime', 't2.nickname as admin_name')
            ->from((new HelpCenter())->getTable() . ' as t1')
            ->leftJoin((new Admin())->getTable() . ' as t2', 't1.admin_id', '=', 't2.id')
            ->where('t1.id', $id)
            ->first();
        if ($help) {
            if (isset($help['createtime'])) {
                $help['createtime'] = date("Y-m-d H:i:s", $help['createtime']);
            }
            if (isset($help['updatetime'])) {
                $help['updatetime'] = date("Y-m-d H:i:s", $help['updatetime']);
            }
        }
        return $this->apiResponse($help);
    }

    public function store(Request $request)
    {
        if (!$this->permissionAuthenticate($request->RESOURCE, "这里输入需要的资源组权限")) {
            return $this->apiResponse('', config('errorLiu.NINZHANWUGAIJIEKOUQUANX'));
        }
        $id = $request->id;
        $data = $request->only('name', 'content');
        if (!isset($data['name']) || !isset($data['content'])) {
            return $this->apiResponse('', config('errorLiu.NOTCANSHUID'));
        }
        $time = time();
        $data['updatetime'] = $time;
        $data['admin_id'] = $request->ADMINID;
        if($id){
            $content = "修改";
            HelpCenter::where('id',$id)->update($data);
        }else{
            $content = "新增";
            $data['createtime'] = $time;
            $id = HelpCenter::insertGetId($data);
        }
        //添加管理员日志开始
        $url = $this->namespace . "/store";
        $title = $content;
        $content .= "了id为【".$id."】的帮助中心,内容为【".json_encode($data)."】";
        $this->addAdminLog($request->ADMINID, $request->ADMINUSERNAME, $url, "帮助中心模块", $title, $content, $request->getClientIp());
        //添加管理员日志结束
        return $this->apiResponse(true);
    }

    public function destroy(Request $request, $id)
    {
        if (!$this->permissionAuthenticate($request->RESOURCE, "这里输入需要的资源组权限")) {
            return $this->apiResponse('', config('errorLiu.NINZHANWUGAIJIEKOUQUANX'));
        }
        $help = HelpCenter::select()->where('id', $id)->first();
        HelpCenter::where('id', $id)->delete();
        //添加管理员日志开始
        $url = $this->namespace . "/destroy";
        $title = "删除";
        $content = "删除了帮助中心的内容为【".json_encode($help)."】";
        $this->addAdminLog($request->ADMINID, $request->ADMINUSERNAME, $url, "帮助中心模块", $title, $content, $request->getClientIp());
        //添加管理员日志结束
        return $this->apiResponse(true);
    }
}